import BasePanel from "./BasePanel";
import { GamePropData } from "../Data/GamePropData";
import CompositeUi from "../UI/GamePanel/CompositePanel/CompositeUi";
import LevelBaseControl from "../LevelCommon/LevelBaseControl";
import CommonConstant from "../../constant/CommonConstant";
import { utils } from "../../utils/CommonUtil";
import EnumData from "../../Enum/EnumData";
import LanguageManager from "../../constant/LanguageManager";
import DialogDataConst from "../../constant/DialogDataConst";

export default class CompositePanel extends BasePanel{

      /** 合成材料image*/
      private compositeUis:Array<CompositeUi> = new Array<CompositeUi>();
      /** 合成材料数据*/
      private materialPropDatas:Array<GamePropData>;

      /** 合成按钮*/
      private compositeBtn:fairygui.GButton;
      /** 合成字*/
      private compositeText:fairygui.GLabel;

      private curLevelControl:LevelBaseControl;

      private titleText:fairygui.GLabel;


    private materialImage1:fairygui.GLoader;

    private materialImage2:fairygui.GLoader;

    constructor(id:number, materialPropDatas:Array<GamePropData>){
        super()
        this.m_nameID = id;
        this.materialPropDatas = materialPropDatas;
   
        this.InitFui();
        this.InitUi();
       
        this.mowner = this.m_fui.displayObject;
        this.mowner.alpha = 0;
        Laya.Tween.to(this.mowner,{alpha:1},500);
    }

    private InitFui(){
        this.m_fui = window.gameFacade.ResourceMng.getMainCom(
            CommonConstant._fuiCompositePanelPath.Package,
            "CompositePanel",
            "CompositePanel");
        
        this.materialImage1 = this.m_fui.getChild("materail1Image").asLoader;
        this.materialImage2 = this.m_fui.getChild("materail2Image").asLoader;

        let compositeUi1 = utils.CommonUtils.CustomAddComponent(this.materialImage1.displayObject,CompositeUi) as CompositeUi;
        let compositeUi2 = utils.CommonUtils.CustomAddComponent(this.materialImage2.displayObject,CompositeUi) as CompositeUi;
        this.compositeUis.push(compositeUi1);
        this.compositeUis.push(compositeUi2);
        
        this.m_fui.getChild("compositeBtn").onClick(this,this.OnClickComposite);
        this.compositeText = this.m_fui.getChild("compositeText").asLabel;
        this.compositeText.text = window.gameFacade.ConfigMng.GetLanguageByKey(LanguageManager.COMPOSITE);

        this.curLevelControl = window.gameFacade.LevelMng.GetCurLevelControl();
        this.titleText = this.m_fui.getChild("titleText").asLabel;
        this.titleText.text = window.gameFacade.ConfigMng.GetLanguageByKey(LanguageManager.COMPOSITE);
        
        this.m_fui.getChild("bg").onClick(this,this.onClose);
        super.Show();
    }

    private InitUi():void
    {
        let length = this.materialPropDatas.length;

            /** 材料道具ui赋值*/
        this.compositeUis[0].SetData(this.materialPropDatas[0],this.materialImage1);
        this.compositeUis[1].SetData(this.materialPropDatas[1],this.materialImage2);
    }

    /** 合成*/
    private OnClickComposite():void
    {
        /** 合成*/
        let gamePropData:GamePropData = window.gameFacade.ConfigMng.CompositeGameProp(this.materialPropDatas);
        /** 合成成功*/
        if (gamePropData != null)
        {
            /** 消耗掉道具材料*/
            this.curLevelControl.UseProp(this.materialPropDatas);
            /** 获得合成的道具*/
            this.curLevelControl.GetGameProp(gamePropData);
        }
        /** 合成失败*/
        else
        {            
            this.curLevelControl.ShowDialog(DialogDataConst.nothing_happened);
        }

        this.onClose();

    }  

    public SetData(materialPropDatas:Array<GamePropData>){
        this.materialPropDatas = materialPropDatas;
    }

    private onClose() {
        window.gameFacade.SceneMng.close(EnumData.EnumPanelType.CompositePanel);
    }
}